package com.kye.express.business.jd.search_list.search_result;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.LifecycleOpenApiResultObserver;
import com.kye.express.contract.base.view.IView;
import com.kye.model.UserSession;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.jd.shop.JDGoodsListResultBean;
import com.kye.model.bean.jd.shop.request.RequestJDGoodsList;
import com.kye.model.bean.user.User;
import com.kye.model.datasource.JDShopDataSource;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 京东兑换-搜索-结果页面 Presenter
 * <p>
 * Created by SkyYu on 2018/1/28
 */
public interface JDGoodsSearchResultContract {

    interface JDGoodsSearchResultView extends IView {
        void onLoadUser(User user);

        void onCompleteJDGoodsList(boolean isLoadMore);

        void onSuccessJDGoodsList(OpenApiResult<JDGoodsListResultBean> bean, boolean isFirstIn, boolean isReFresh,boolean isLoadMore);

        void onApiErrorJDGoodsList(OpenApiResult<JDGoodsListResultBean> result, int code, String msg);

        void onOtherErrorJDGoodsList(Throwable e);
    }

    class JDGoodsSearchResultPresenter extends BasePresenter<JDGoodsSearchResultView> {

        JDShopDataSource jdShopDataSource;

        JDGoodsSearchResultPresenter() {
            jdShopDataSource = new JDShopDataSource();
        }

        @Override
        public void bind(JDGoodsSearchResultContract.JDGoodsSearchResultView view) {
            super.bind(view);
            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onLoadUser(user);
                        }
                    });
        }

        void requestToJDGoodsList(boolean isFirstIn, boolean isReFresh, boolean isLoadMore, String phone, String goodsName, int pageIndex, int pageSize, String KySign) {
            RequestJDGoodsList requestJDGoodsList = new RequestJDGoodsList();
            requestJDGoodsList.setPhone(phone);
            requestJDGoodsList.setGoodsName(goodsName);
            requestJDGoodsList.setPageIndex(pageIndex);
            requestJDGoodsList.setPageSize(pageSize);
            requestJDGoodsList.setKySign(KySign);
            jdShopDataSource.getJDGoodsList(requestJDGoodsList)
                    .compose(getView().bindToLifecycleDestroy())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new LifecycleOpenApiResultObserver<JDGoodsListResultBean>(this) {
                        @Override
                        public void onStart(Disposable d) {

                        }

                        @Override
                        public void onLoaded() {
                            getView().onCompleteJDGoodsList(isLoadMore);
                        }

                        @Override
                        public void onSuccess(OpenApiResult<JDGoodsListResultBean> result, JDGoodsListResultBean jdGoodsListResultBean) {
                            getView().onSuccessJDGoodsList(result, isFirstIn, isReFresh,isLoadMore);
                        }

                        @Override
                        public void onApiError(OpenApiResult<JDGoodsListResultBean> result, int code, String msg) {
                            getView().onApiErrorJDGoodsList(result, code, msg);
                        }

                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onOtherErrorJDGoodsList(e);
                        }
                    });
        }

        public void refresh(boolean isFirstIn, boolean isReFresh, boolean isLoadMore, String phone, String goodsName, int pageIndex, int pageSize, String KySign) {
            requestToJDGoodsList(isFirstIn, isReFresh, isLoadMore, phone, goodsName, pageIndex, pageSize, KySign);
        }

        public void loadMore(boolean isFirstIn, boolean isReFresh, boolean isLoadMore, String phone, String goodsName, int pageIndex, int pageSize, String KySign) {
            requestToJDGoodsList(isFirstIn, isReFresh, isLoadMore, phone, goodsName, pageIndex, pageSize, KySign);
        }
    }
}